package com.ysd.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ysd.dao.impl.ChanPinFenLeiDaoImpl;
import com.ysd.dao.impl.ChanPinXinXiDaoImpl;
import com.ysd.entity.ChanPinFenLei;
import com.ysd.entity.ChanPinXinXi;

public class ProductsServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//产品分类
		ChanPinFenLeiDaoImpl cpDao=new ChanPinFenLeiDaoImpl();
		List<ChanPinFenLei> cpList= cpDao.getChanPinFenLeiList();
		request.setAttribute("cpList", cpList);
		
		//产品信息
		//根据名字模糊查询
		String cName=request.getParameter("cName");
		String fenLeiID= request.getParameter("fenLeiID");
		ChanPinXinXiDaoImpl cpxxDao=new ChanPinXinXiDaoImpl();
		
		if(fenLeiID!=null) {//如果fenLeiID不为null，那么代表分类id查询
			int fenLeisID=Integer.parseInt(fenLeiID);//转化为int
			List<ChanPinXinXi> cpxxList=cpxxDao.getCPXXFenLeiId(fenLeisID);
			request.setAttribute("cpxxList",cpxxList );
			request.setAttribute("fenLeiID", fenLeiID);
		}else {//否者模糊查询
			List<ChanPinXinXi> cpxxList=cpxxDao.getCpXXCName(cName);
			request.setAttribute("cpxxList",cpxxList );
		}
		int cpFlId=0;
		if(request.getParameter("fenLeiID")!=null && !request.getParameter("fenLeiID").equals("")) {
			cpFlId=Integer.parseInt(request.getParameter("fenLeiID"));
		}
		
		List<ChanPinFenLei> dao= cpDao.getChanPinFLList(cpFlId);
		System.out.println(cpFlId);
		request.setAttribute("dao", dao);
		
		
		
		
		
		request.getRequestDispatcher("productlist.jsp").forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
